import api from "@/api";
import { ElMessage } from "element-plus";
import { ComputedRef, ref } from "vue";

// 数据列表
interface UseDataList {
	api: Function;
	params?: any;
	deleteApi?: Function;
}
export const useDataList = (props: ComputedRef<UseDataList>) => {
	const list = ref<any[]>([]);
	const total = ref(0);
	const page = ref(1);

	const getList = async () => {
		const msg = await props.value.api({
			pageNum: page.value,
			pageSize: 20,
			...(props.value.params || {}),
		});
		if (msg.code == 1000) {
			list.value = msg.data.list || [];
			total.value = msg.data.total || 0;
			console.log("list", list.value);
		}
	};

	const deleteData = async (params: any) => {
		const msg = await props.value?.deleteApi?.(params);
		if (msg?.code == 1000) {
			ElMessage.success("删除成功");
			getList();
		}
	};

	return {
		list,
		total,
		page,
		getList,
		deleteData,
	};
};

// 角色列表
export const useRoleList = () => {
	const role_list = ref<
		{
			id: string;
			organizeCode: "BLUE";
			roleCode: string;
			roleName: string;
		}[]
	>([]);

	const getRoleList = async () => {
		const msg = await api.role.getRoleList();
		if (msg.code == 1000) {
			console.log(msg);
			role_list.value = msg.data || [];
		}
	};
	getRoleList();

	return {
		role_list,
		getRoleList,
	};
};
